package org.hefan.org.service.impl;

import org.hefan.basic.exception.BusinessException;
import org.hefan.basic.service.impl.BaseServiceImpl;
import org.hefan.org.domain.Employee;
import org.hefan.org.domain.Shop;
import org.hefan.org.mapper.EmployeeMapper;
import org.hefan.org.mapper.ShopMapper;
import org.hefan.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * ShopServiceImpl实现类
 * 先继承再实现
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public void settlement(Shop shop) {
        //1.基本信息审核 - 当前店铺入驻过，就不能再入驻
        //1.1.如何判断当前店铺入驻过 - 地址 + 店铺的名称
//        Shop dbShop = shopMapper.findByAddressAndName(shop);
//        if (dbShop != null) { // 表示入驻过
//            throw new BusinessException("入驻失败，你已经入驻过，请直接登录，忘记密码请找回!!!");
//        }

        //1.基本信息审核 - 当前店铺入驻过，就不能再入驻【需求：一个店铺管理员就只能开一个店】
        Employee employee = employeeMapper.findByAccount(shop.getAdmin());
        if (employee != null) { // 表示这个人以前入驻过
            throw new BusinessException("入驻失败，你已经入驻过，请直接登录，忘记密码请找回!!!");
        } else {
            //2.店铺入驻
            //2.1. 先保存employee信息 - 可以获取到自增长的id
            employeeMapper.save(shop.getAdmin()); //shop对象中的admin对象的id  有值了

            //2.2. 再保存shop信息 - 在shop中绑定管理员id
            shopMapper.save(shop); //shop对象中的admin对象的id 有值

            //2.3. 最后在管理员中绑定店铺id【根据employee_id 去修改 shop_id】
            employeeMapper.updateShopId(shop);
        }
    }
}
